<?php

class User_Control extends Controller {
	
	var $user;	
	var $last;
	var $base;
	var $css;
	var $message;
	var $upload_path;
	var $admin;
	
	function User_Control()
	{
		parent::Controller();
		$this->load->model('Quick_Tip');
		$status = $this->session->userdata('status');
		if(!isset($status) || $status != 'OK') {
			redirect('onlineaide');
		} else {

			$this->base = $this->config->item('base_url');
			$this->css = $this->config->item('css');
			$this->upload_path = $this->config->item('uploads');
			$a = $this->Quick_Tip->random_tip();
			$this->message= $a['TipDescription'];
			$this->user = $this->session->userdata('user');
			if ($this->session->userdata('admin') == 0) {
				$this->admin = FALSE;
			} else {
				$this->admin = TRUE;
			}
			$this->load->model('User');
			$query = $this->User->getLastLogOut($this->user);
			if ($query->num_rows() > 0)
			{
   				$row = $query->row_array();
				//$date = $row['Last'];
				$this->last= $row['Last'];
				/*if($date == "0000-00-00") {
					$this->last="This is your first visit!";
				} else {
					$this->last = $row['LastLogOut'];}*/
			}
			
		}
	}

	function index()
	{
		redirect('user_control/home');
	}
	
	function home()
	{
		$this->load->model('Events');
		$this->load->model('Subjects');
		$this->load->model('Submissions');
		$events = $this->Events->select_latest(3);
		$top10 = $this->Subjects->get_top_10();
		$top5 =  $this->Submissions->get_latest_7();
		$data['events'] = $events;
		$data['top10'] = $top10;
		$data['top5'] = $top5;
		$data['user'] = $this->user;
		$data['mywebtitle'] = 'Home - '.$this->user;
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/home_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper" >';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function subjects()
	{
		$data['user'] = $this->user;
		$data['mywebtitle'] = "DCSOA Subjects";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/subjects_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper" style="width: 678px; min-height: 1050px; _height: 1050px; background: url(images/header.jpg) no-repeat; position: relative;">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function members()
	{
		$this->load->model('Members');
		$members = $this->Members->select_all();
		$data['error'] = $this->session->flashdata('error');
		$data['members'] = $members;
		$data['admin'] = $this->admin;
		$data['user'] = $this->user;
		$data['mywebtitle'] = 'DCSOA Members';
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/members_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function mentors()
	{
		$this->load->model('Mentors');
		$mentors11 = $this->Mentors->select_all('CS 11');
		$mentors12 = $this->Mentors->select_all('CS 12');
		$data['error'] = $this->session->flashdata('error');
		$data['admin'] = $this->admin;
		$data['mentors11'] = $mentors11;
		$data['mentors12'] = $mentors12;
		$data['user'] = $this->user;
		$data['mywebtitle'] = 'Home - '.$this->user;
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/mentors_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}
	
	function cp()
	{	$data['error'] = $this->session->flashdata('error');
		$this->load->model('Members');
		$profile = $this->Members->select_profile($this->user);
		$data['profile'] = $profile;
		$data['user'] = $this->user;
		$data['mywebtitle'] = $this->user.' Control Panel';
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/cp_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function edit_profile() {
		$this->load->model('Members');
		$array = $this->Members->select_profile_wo_email($this->user);
		$data['array'] = $array;
		$data['user'] = $this->user;
		$data['mywebtitle'] = $this->user.' Control Panel';
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/edit_profile_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper" style="width: 678px;
    min-height: 650px;
    _height: 650px;
    position: relative;">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function do_edit_profile() {
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['firstName'] = "required|alpha_dash_space";
		$rules['middleName'] = "alpha_dash_space";
		$rules['lastName'] = "required|alpha_dash_space";
		$rules['address'] = "alpha_dash_space";
		$rules['idNumber'] = "alpha_dash";
		$rules['contactNo'] = "numeric|min_length[7]";
		$rules['course'] = "min_length[2]|alpha_dash_space";
		$rules['yearLevel'] = "integer|exact_length[1]";

		$this->validation->set_rules($rules);

		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE)
		{
			$this->edit_profile();
		} else {

			$this->load->model('User');
			$array = array('FirstName'=>$this->input->post('firstName'), 'MiddleName'=>$this->input->post('middleName'), 'LastName'=> $this->input->post('lastName'), 'Address'=>$this->input->post('address'),'ContactNo'=>$this->input->post('contactNo'), 'IDNo'=>$this->input->post('idNumber'), 'YearLevel'=>$this->input->post('yearLevel'), 'Course'=>$this->input->post('course'));
			$this->User->update_profile($this->user, $array);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully edited your profile!</div>');
			redirect('user_control/cp');
		}
	}

	function change_email() {
		$data['user'] = $this->user;
		$data['mywebtitle'] = $this->user.' Change Email';
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/change_email_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper" style="width: 678px;
    min-height: 500px;
    _height: 500px;
    position: relative;">';
		
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function do_change_email() {
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['newEmail'] = "required|valid_email|callback_email_check|callback_oldnew_check";
		$this->validation->set_rules($rules);
		
		if ($this->validation->run() == FALSE)
		{
			$this->change_email();
		} else {

			//$this->load->model('User');
			//$ok = $this->User->change_authenticate_email($this->user, $this->input->post('oldEmail'));
			//if($ok != 0) {
				$this->User->update_email($this->user, $this->input->post('newEmail'));
				$this->session->set_flashdata('error', '<div class="nerror">You have successfully changed your email!</div>');
				redirect('user_control/cp');
			//} else {
			//	redirect('user_control/change_email');
			//}
		}
	}

	function email_check($email)
	{
		$this->load->model('User');
		$exists = $this->User->reg_authenticate_email($email);
		if($exists != 0) {
			$this->validation->set_message('email_check', 'The %s is already existing.');
			return FALSE;
		} else { return TRUE; }
	}
	
	function oldnew_check($email)
	{
		$this->load->model('User');
		$ok = $this->User->change_authenticate_email($this->user, $this->input->post('oldEmail'));
		if($ok != 0) {
			return TRUE;
		} else {
			$this->validation->set_message('oldnew_check', 'Old email does not match.');
			return FALSE;
		}			
	}


	function change_password() {
		$data['user'] = $this->user;
		$data['mywebtitle'] = $this->user.' Change Password';
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/change_password_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper" style="width: 678px;
    min-height: 500px;
    _height: 500px;
    position: relative;">';
		
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function oldpass_check($password) {
		$this->load->model('User');
		$ok = $this->User->change_authenticate_password($this->user, sha1($password));
		if($ok != 0) {
				return TRUE;
		} else {
				$this->validation->set_message('oldpass_check', 'Old password does not match your current password');
				return FALSE;
		}
	}

	function do_change_password() {
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['oldPassword'] = "required|callback_oldpass_check";
		$rules['newPassword'] = "required|matches[confNewPassword]|min_length[6]|max_length[20]";
		$rules['confNewPassword'] = "required";
		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE)
		{
			$this->change_password();
		} else {
		/*if($this->input->post('newPassword') == $this->input->post('confNewPassword')) {
			$this->load->model('User');
			$ok = $this->User->change_authenticate_password($this->user, sha1($this->input->post('oldPassword')));
			if($ok != 0) {*/
				$this->User->update_password($this->user, sha1($this->input->post('newPassword')));
				$this->session->set_flashdata('error', '<div class="nerror">You have successfully changed your password!</div>');
				redirect('user_control/cp');
			/*} else {
				redirect('user_control/change_password');
			}
		} else {
			redirect('user_control/change_email');		
		}*/
		}
	}

	function mentor_check($subj)
	{
		$this->load->model('Mentors');
		$array = array('Username'=>$this->user, 'Subject'=>$subj);
		$ok = $this->Mentors->is_a_mentor($array);
		if($ok == 0) {
				return TRUE;
		} else {
				$this->validation->set_message('mentor_check', 'You are already a mentor for '.$subj);
				return FALSE;
		}
	}

	function mentor_signup()
	{	
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['subsel'] = "callback_mentor_check";
		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE)
		{
			$this->mentors();
		} else {
			$array = array('Username'=>$this->user, 'Subject'=>$this->input->post('subsel'));
			$this->load->model('Mentors');
			$this->Mentors->sign_up($array);
			redirect('user_control/mentors');
		}
	}

	function member_profile($username)
	{
		$this->load->model('Members');
		$member = $this->Members->select_profile($username);
		$data['profile'] = $member;
		$data['mywebtitle'] = "Viewing ".$username."'s Profile";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['user']  = $this->user;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/member_profile_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function subjects_profile($subject, $resource = 0) {
		$data['user'] = $this->user;
		$data['subject'] = $subject;
		$data['resource'] = $resource;
		$data['admin'] = $this->admin;
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['lastvisit'] = $this->last;
		$data['error'] = $this->session->flashdata('message');
		$data['error_for_delete'] = $this->session->flashdata('error');
		if($resource == 0) {
			$this->load->model(array('Subjects', 'Submissions'));
			$profile = $this->Subjects->select_subject($subject);
			$resources = $this->Submissions->select_all_subject_resources($subject);
			$data['profile'] = $profile;
			$data['images'] = $this->config->item('img');
			$data['resources'] = $resources;
			$data['subject'] = $subject;
			$data['mywebtitle'] = $subject.' Information';
			$data['body'] = $this->load->view('template/subject_profile_body', $data, TRUE);
			$data['wrapper'] = '<div id = "wrapper" style="width: 678px; min-height: 950px; _height: 950px; position: relative;">';
			$this->load->view('home_view', $data);
		} else {
			$this->load->model('Submissions');
			$array = array('Subject'=>$subject, 'ResourceIDNo'=>$resource);
			$num = $this->Submissions->authenticate_resource_id($array);

			if($num != 0) {
				$resource = $this->Submissions->select_subject_resource($resource);
				$data['resource'] = $resource;
				$data['subject'] = $subject;
				$data['mywebtitle'] = $subject.' Resource';
				$data['body'] = $this->load->view('template/subject_resource_body', $data, TRUE);
				$data['wrapper'] = '<div id = "wrapper">';
				$this->load->view('home_view', $data);
			} else {
				redirect('user_control/subjects_profile/'.$subject);
			}
		}
	}

	function rate() {
		$this->load->model('User');
		$subject = $this->input->post('subject');
		$rating = $this->input->post('rating');
		$num = $this->User->rate_authenticate($this->user, $subject);
		if($num == 0) {
			$this->load->model('Rating');
			$array = array('Subject'=>$subject, 'Rating'=>$rating, 'Username'=>$this->user);
			$no_error = $this->Rating->rate($array);
			if($no_error) {
				$this->session->set_flashdata('message', '<div class="nerror">You have successfully rated the subject</div>');
				redirect('user_control/subjects_profile/'.$subject);
			} else {
				$this->session->set_flashdata('message', '<div class="error">You have already rated the subject.</div>');
				redirect('user_control/subjects_profile/'.$subject);
			}
		} else {
			$this->session->set_flashdata('message', '<div class="error">You have already rated the subject.</div>');
			redirect('user_control/subjects_profile/'.$subject);
		}
	}

	function events()
	{
		$this->load->model('Events');
		$events = $this->Events->select_all();
		$data['admin'] = $this->admin;
		$data['error'] = $this->session->flashdata('error');
		$data['events'] = $events;
		$data['user'] = $this->user;
		$data['mywebtitle'] = "DCSOA News and Events";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/events_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function submissions()
	{
		$this->load->model('Submissions');
		$submissions = $this->Submissions->select_all();
		$data['submissions'] = $submissions;
		$data['error'] = $this->session->flashdata('error');
		$data['user'] = $this->user;
		$data['admin'] = $this->admin;
		$data['mywebtitle'] = "DCSOA Subject Resource Submissions";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/submissions_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function logout()
	{
		$this->load->model('User');
		//update last logout
		$this->User->update(array('LastLogOut'=>date("Y-m-d H:i:s")), $this->user);
		setcookie('dcsoa_session', '', time() - 3600, '/'); // destroy cookie
		$this->session->sess_destroy();
		redirect('onlineaide');
	}

	function add_resource($subject)
	{
		
		$type = $this->input->post('res_type');
		if($type === 'file') {redirect('user_control/add_resource_file/'.$subject);}
		$data['subject'] = $subject;
		$data['user'] = $this->user;
		$data['mywebtitle'] = $subject." - Submit a Resource";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		//if($type == 'link' || $type == 'reference') {
			$data['selected'] = $type;
			$data['body'] = $this->load->view('template/add_resource_body', $data, TRUE);
		//} else {
		//	$data['body'] = $this->load->view('template/add_file_resource_body', $data, TRUE);
		//}
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function add_resource_file($subject)
	{
		$data['error'] = $this->session->flashdata('error');
		$data['subject'] = $subject;
		$data['user'] = $this->user;
		$data['mywebtitle'] = $subject." - Submit a File";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/add_file_resource_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function do_upload()
	{
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['location'] = "required";
		$this->validation->set_rules($rules);

		if ($this->validation->run() == FALSE)
		{
			$this->add_resource($this->input->post('subject'));
		} else {

			$type = $this->input->post('drop');
			$subject = $this->input->post('subject');
			$title = $this->input->post('title');
			$location = $this->input->post('location');
			$author = $this->input->post('author');
			$array = array('Username'=>$this->user, 'Subject'=>$subject, 'Title'=>$title, 'Location'=>$location, 'Author'=>$author, 'Type'=>$type);

			$this->load->model('Submissions');
			$this->Submissions->submit_resource_no_file($array);
			redirect('user_control/subjects_profile/'.$subject);
		}
	}

	function do_upload_with_file()
	{
		$this->load->library('validation');
		$this->validation->set_error_delimiters('<div class="error">', '</div>');

		$rules['title'] = "required|alpha_dash_space";
		$this->validation->set_rules($rules);
		if ($this->validation->run() == FALSE)
		{
			$this->add_resource_file($this->input->post('subject'));
		} else {
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png|txt|pdf|htm|html|bmp';
			$config['max_size']	= '2048';
			$config['max_width']  = '0';
			$config['max_height']  = '0';

			$this->load->model('Submissions');
		
			$this->load->library('upload', $config);
	
			if ( ! $this->upload->do_upload())
			{
				$error = $this->upload->display_errors();
				$this->session->set_flashdata('error', '<div class="error">'.$error.'</div>');
				redirect('user_control/add_resource_file/'.$this->input->post('subject'));
			}	
			else
			{
				$this->load->helper('file');
				$up_data = $this->upload->data();
				$data = array('upload_data' => $up_data);
				$blob = read_file($up_data['full_path']);
				$array1 = array('name'=>$up_data['file_name'], 'type'=>$up_data['file_type'], 'size'=>$up_data['file_size'], 'content'=>$blob);
				$array2 = array('Username'=>$this->user, 'Subject'=>$this->input->post('subject'), 'Title'=>$this->input->post('title'), 'Location'=>'Server', 'Author'=>$this->input->post('author'), 'Type'=>'File');
				$this->Submissions->submit_resource($array1, $array2);
				$this->session->set_flashdata('error', '<div class="error">You have successfully submitted a file.</div>');
				redirect('user_control/add_resource_file/'.$this->input->post('subject'));
			}
		}
	}

	function download()
	{
		$id = $this->input->post('UploadIDNo');
		$this->load->model('Submissions');
		$array = $this->Submissions->get_blob($id);
		force_download($array['name'], $array['content']);
	}

	function add_event()
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->helper('smiley');
		$this->load->library('table');
		
		$image_array = get_clickable_smileys($this->config->item('base_url').'/'.$this->config->item('img').'/smileys');

		$col_array = $this->table->make_columns($image_array, 8);		
			
		$data['smiley_table'] = $this->table->generate($col_array);
		
			$data['admin'] = $this->admin;
			$data['error'] = $this->session->flashdata('error');
			$data['user'] = $this->user;
			$data['mywebtitle'] = "DCSOA Add an Event/News";
			$data['base'] = $this->base;
			$data['css']  = $this->css;
			$data['message'] = $this->message;
			$data['body'] = $this->load->view('template/add_event_body', $data, TRUE);
			$data['wrapper'] = '<div id = "wrapper">';
			$data['lastvisit'] = $this->last;
			$this->load->view('home_view', $data);
		}
	}

	function do_add_event()
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->library('validation');
			$this->validation->set_error_delimiters('<div class="error">', '</div>');

			$rules['event'] = "required";
			$this->validation->set_rules($rules);
			if ($this->validation->run() == FALSE)
			{
				$this->add_event();
			} else {
				$array = array('EventDescription'=>$this->input->post('event'));
				
				$this->load->model('Events');
				$this->Events->add($array);
				$this->session->set_flashdata('error', '<div class="nerror">You have successfully added an event/news</div>');
				redirect('user_control/add_event');
			}
		}
	}

	function delete_event($id)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('Events');
			$this->Events->delete($id);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully deleted an event</div>');
			redirect('user_control/events');
		}
	}

	function delete_submission($id)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('Submissions');
			$this->Submissions->delete($id);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully deleted a submission</div>');
			redirect('user_control/submissions');
		}
	}

	function delete_submission_from_subject($subject, $id)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('Submissions');
			$this->Submissions->delete($id);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully deleted a subject resource</div>');
			redirect('user_control/subjects_profile/'.$subject);
		}
	}

	function delete_member($username)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('User');
			$this->User->delete($username);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully deleted a member</div>');
			redirect('user_control/members');
		}
	}

	function delete_mentorship($id)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('Mentors');
			$this->Mentors->delete($id);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully disabled a user from being a mentor</div>');
			redirect('user_control/mentors');
		}
	}

	function view_instructors($subject)
	{
		$this->load->model('Instructor');
		$instructors = $this->Instructor->select($subject);
		$data['instructors'] = $instructors;
		$data['subject'] = $subject;
		$data['error'] = $this->session->flashdata('error');
		$data['user'] = $this->user;
		$data['admin'] = $this->admin;
		$data['mywebtitle'] = $subject." - Instructor List";
		$data['base'] = $this->base;
		$data['css']  = $this->css;
		$data['message'] = $this->message;
		$data['body'] = $this->load->view('template/instructor_list_body', $data, TRUE);
		$data['wrapper'] = '<div id = "wrapper">';
		$data['lastvisit'] = $this->last;
		$this->load->view('home_view', $data);
	}

	function add_instructor($subject)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$data['admin'] = $this->admin;
			$data['error'] = $this->session->flashdata('error');
			$data['user'] = $this->user;
			$data['mywebtitle'] = $subject." - Add Instructor";
			$data['base'] = $this->base;
			$data['subject'] = $subject;
			$data['css']  = $this->css;
			$data['message'] = $this->message;
			$data['body'] = $this->load->view('template/add_instructor_body', $data, TRUE);
			$data['wrapper'] = '<div id = "wrapper">';
			$data['lastvisit'] = $this->last;
			$this->load->view('home_view', $data);
		}
	}

	function instructor_check($name) {
		$subject = $this->input->post('subject');
		$this->load->model('Instructor');
		$array = array('Subject'=>$subject, 'InstructorName'=>$name);
		$exists = $this->Instructor->authenticate($array);
		if($exists != 0) {
			$this->validation->set_message('instructor_check', 'The %s is already existing for '.$subject);
			return FALSE;
		} else { return TRUE; }
	}

	function do_add_instructor()
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->library('validation');
			$this->validation->set_error_delimiters('<div class="error">', '</div>');

			$rules['instructor'] = "required|callback_instructor_check|alpha_dash_space";
			$this->validation->set_rules($rules);

			if ($this->validation->run() == FALSE)
			{
				$this->add_instructor($this->input->post('subject'));
			} else {
				$array = array('InstructorName'=>$this->input->post('instructor'), 'Subject'=>$this->input->post('subject'));
				
				$this->load->model('Instructor');
				$this->Instructor->add($array);
				$this->session->set_flashdata('error', '<div class="nerror">You have successfully added an instructor for '.$this->input->post('subject').'</div>');
				redirect('user_control/add_instructor/'.$this->input->post('subject'));
			}
		}
	}

	function delete_instructor($subject, $id)
	{
		if(! $this->admin) {
			redirect('user_control/home');
		} else {
			$this->load->model('Instructor');
			$this->Instructor->delete($id);
			$this->session->set_flashdata('error', '<div class="nerror">You have successfully deleted an instructor</div>');
			redirect('user_control/view_instructors/'.$subject);
		}
	}

}
?>
